package com.mo.lib.ui.dialog

import android.app.Dialog
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import com.xuexiang.xutil.display.ScreenUtils

/**
 * @ author：mo
 * @ data：2021/9/28:8:58
 * @ 功能：弹窗模板
 * 注意：当通过构造方法传进数据的时候，要在init方法里处理视图和数据，doWhat里只能处理静态数据
 */

/** Dialog消失类型*/
enum class DismissType {
    /** 代码控制取消*/
    NEVER,

    /** 系统返回键取消*/
    BACK,

    /** 点视图区域外取消*/
    OUT,

    /** 系统返回键、点视图区域外取消*/
    OTHER
}

 abstract class BaseDialog(private val layoutId: Int) {
    val dialog = KBaseDialog()

    init {
        dialog.setContentView(layoutId)
        dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        setDialogScale(0.8)
        setDimAmount(0.4)
        setDismissType(DismissType.NEVER)
        doWhat()

    }

    abstract fun doWhat()

    /**
     * 开启
     */
    open fun show() {
        if (!dialog.isShowing) {
            dialog.show()
        }
    }

    /**
     * 关闭
     */
    open fun dismisss() {
        if (dialog.isShowing) {
            dialog.cancel()
            dialog.dismiss()

        }
    }


}

class BaseDialog2(layoutId: Int,val block:(KBaseDialog)->Unit):BaseDialog(layoutId){
    init {
        dialog?.let {
            block.invoke(dialog)
        }

    }
    override fun doWhat() {

    }
}

/**设置消失类型    不设置默认点视图外区域或返回键均取消*/
fun BaseDialog.setDismissType(dismissType: DismissType): BaseDialog = apply {
    dialog.setCanceledOnTouchOutside(false)
    when (dismissType) {
        DismissType.NEVER -> { //api代码控制
            dialog.setCancelable(false)
        }
        DismissType.BACK -> { //返回键
            dialog.setCancelable(true)
        }
        DismissType.OUT -> { //视图区域外
            dialog.setCancelable(false)
            dialog.outCanAble = true
        }
        DismissType.OTHER -> { //视图区域外
            dialog.setCancelable(true)
            dialog.outCanAble = true
        }
    }
}

/** 设置监听*/
fun BaseDialog.setDialogListener(dialogCallBack: DialogCallBack): BaseDialog {
    dialog.setOnCancelListener { dialogInterface: DialogInterface -> dialogCallBack.onCancel(dialogInterface) } //            dialog取消监听
    dialog.setOnShowListener { dialogInterface: DialogInterface -> dialogCallBack.onShow(dialogInterface) } //       dialog显示监听
    dialog.setOnDismissListener { dialogInterface: DialogInterface -> dialogCallBack.onDismiss(dialogInterface) } //     dialog消失监听
    dialog.setOnKeyListener { dialogInterface: DialogInterface, i: Int, keyEvent: KeyEvent -> dialogCallBack.onKey(dialogInterface, i, keyEvent) } //            监听系统返回键
    return this
}
 fun BaseDialog.setOnCancelListener(block:(DialogInterface)->Unit): BaseDialog {
    dialog.setOnCancelListener(block)
    return this
}
 fun BaseDialog.setOnShowListener(block:(DialogInterface)->Unit): BaseDialog {
    dialog.setOnShowListener(block)
    return this
}
fun BaseDialog.setOnDismissListener(block:(DialogInterface)->Unit): BaseDialog {
    dialog.setOnDismissListener(block)
    return this
}


/** 设置沉浸状态栏,默认没有重力偏向*/
fun BaseDialog.setImmerse(gravity: Int = Gravity.NO_GRAVITY): BaseDialog {
    dialog.window?.apply {
        val lp: WindowManager.LayoutParams = attributes
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        lp.gravity = Gravity.CENTER //兼容刘海屏
        if (Build.VERSION.SDK_INT >= 28) {
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
        }
        addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        attributes = lp
    }

    return this
}

/**设置dialog宽与屏幕宽之间的比例  0最小 1最大 */
fun BaseDialog.setDialogScale(scale: Double): BaseDialog {
    dialog.window?.apply {
        val lp: WindowManager.LayoutParams = attributes
        lp.width = (ScreenUtils.getScreenWidth() * scale).toInt()
        attributes = lp
    }

    return this
}

/**设置dialog重力 */
fun BaseDialog.setDialogGravity(gravity: Int): BaseDialog {
    dialog.window?.setGravity(gravity)
    return this
}

/** 设置黑暗度（Dialog自身的黑暗度.也就是透明度）*/
fun BaseDialog.setDimAmount(dimAmount: Double): BaseDialog {
    dialog.window?.apply {
        val lp = attributes
        lp.dimAmount = dimAmount.toFloat()
        attributes = lp
    }
    return this
}

open class DialogCallBack : DialogInterface, DialogInterface.OnCancelListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnKeyListener,
    DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnShowListener {
    override fun cancel() {}
    override fun dismiss() {}
    override fun onCancel(dialogInterface: DialogInterface) {}
    override fun onClick(dialogInterface: DialogInterface, i: Int) {}
    override fun onDismiss(dialogInterface: DialogInterface) {}
    override fun onKey(dialogInterface: DialogInterface, i: Int, keyEvent: KeyEvent): Boolean {
        return false
    }

    override fun onClick(dialogInterface: DialogInterface, i: Int, b: Boolean) {}
    override fun onShow(dialogInterface: DialogInterface) {}
}